package com.rcklos.NettyLentMe;

import com.rcklos.NettyLentMe.match.MatchContainer;
import com.rcklos.NettyLentMe.protocol.protobuf.MessageProtobuf;
import com.rcklos.NettyLentMe.netty.handler.ServerHandler;
import io.netty.channel.Channel;

public class ServerTransponder {
    public static ServerTransponder instance = new ServerTransponder();
    private ServerTransponder(){}
    public static ServerTransponder getInstance(){
        return instance;
    }

    public MessageProtobuf.Msg genMsg(int msgType , String fromId , String toId , String extend , String body ){
        return genMsg(msgType,fromId,toId,0,0,extend,body);
    }

    public MessageProtobuf.Msg genMsg(int msgType , String fromId , String toId , long timestamp , int statusReport , String extend , String body){
        MessageProtobuf.Msg.Builder builder = MessageProtobuf.Msg.newBuilder();
        MessageProtobuf.Head.Builder head = MessageProtobuf.Head.newBuilder();
        head.setMsgType(msgType);
        if (fromId!=null)head.setFromId(fromId);
        if (toId!=null)head.setToId(toId);
        head.setTimestamp(timestamp);
        head.setStatusReport(statusReport);
        if (extend!=null)head.setExtend(extend);
        builder.setHead(head.build());
        if (body!=null)builder.setBody(body);
        return builder.build();
    }

    public void respond( String toid , String message ){
        sendMsg(genMsg(ServerConfig.typeRespond,null,toid,message,null));
    }

    public void sendMsg(MessageProtobuf.Msg msg){
        String toId = msg.getHead().getToId();
        if ( toId == null || toId.isEmpty() ){
            System.out.println("都不知道发给谁，发了个寂寞...");
            return;
        }
        Channel channel = ServerHandler.ChannelContainer.getInstance().getChannelByUserId(toId);
        if ( channel == null ){
            System.out.println("用户["+toId+"]都掉线了，发不出去...");
            return;
        }
        channel.writeAndFlush(msg);
    }

    public void sendInformTheTokenIsExpired(String toid){
        sendMsg(genMsg(ServerConfig.typeInformTheTokenIsExpired,null,toid,null,null));
    }

    public void sendInformTheSingleTokenDestroy(String toid){
        sendMsg(genMsg(ServerConfig.typeInformTheSignTokenDestroy,null,toid,null,null));
    }

    public void sendStringSingleChatTime(String toid){
        String token = MatchContainer.getInstance().getTokenByUserid(toid);
        if ( token == null ) {
            sendInformTheSingleTokenDestroy(toid);
            return;
        }
        long timeLong = MatchContainer.getInstance().getTimeMillisFromToken(token);
        System.out.println("timeLong="+timeLong);
        if ( timeLong == 0 ){
            sendInformTheSingleTokenDestroy(toid);
            return;
        } else if ( System.currentTimeMillis() - timeLong > 1000*60*5 ){
            // 聊天到时间了
            MatchContainer.getInstance().removeTokenByUserid(toid);
            sendInformTheTokenIsExpired(toid);
            return;
        }
        String timeString = String.valueOf(System.currentTimeMillis());
        sendMsg(genMsg(ServerConfig.typeGetSingleChatTime,null,toid,timeString,null));
    }

}
